#include "check_file.h"
#include <stdio.h>

int Check_File::diff_two_file(string filename_1, string filename_2) {
  int res = -1;
  cout<<"File I : "<<filename_1<<endl;
  cout<<"File II : "<<filename_2<<endl;

  std::ifstream file_1;
  file_1.open(filename_1);
  if (!file_1) {
    std::cout << "wrong at Input filename_1 path" << std::endl;
    //exit(0);
    return res;
  }
  file_1.close();

  std::ifstream file_2;
  file_2.open(filename_2);
  if (!file_2) {
    std::cout << "wrong at Input filename_2 path" << std::endl;
    //exit(0);
    return res;
  }
  file_2.close();

  // 文件大小比较
  FILE *pFile;
  char *file;
  pFile = fopen(filename_1.data(), "rb"); //获取已打开文件的指针
  fseek(pFile, 0, SEEK_END); //先用fseek将文件指针移到文件末尾
  int file_size_1 = ftell(pFile); //再用ftell获取文件内指针当前的文件位置。
  fclose(pFile);
  //这个位置就是文件大小。
  // cout<<n<<endl;
  pFile = fopen(filename_2.data(), "rb"); //获取已打开文件的指针
  fseek(pFile, 0, SEEK_END); //先用fseek将文件指针移到文件末尾
  int file_size_2 = ftell(pFile); //再用ftell获取文件内指针当前的文件位置。
  fclose(pFile);

  if (file_size_1 != file_size_2) {
    cout << "!!!  The File Size is different  !!!" << endl;
    cout << "\t File_1 size : " << file_size_1
         << "\t File_2 size : " << file_size_2 << endl;
    return res;
  }

  file_1.open(filename_1);
  file_2.open(filename_2);

  char u1[256]; // unuse
  char u2[256]; // unuse
  int num = 0;
  while (!file_1.eof() && !file_2.eof()) {
    file_1.getline(u1, 256, '\n');
    file_2.getline(u2, 256, '\n');
    num++;
    if( strcmp(u1,u2) != 0){
      cout<<"line_number : "<<num<<" is different !!! "<<endl;
      cout<<" File_1 : "<<u1<<endl;
      cout<<" File_2 : "<<u2<<endl;
      return res;
      break;
    }
  }
  res = 1;
  cout<<" Two File is Equal !!! "<<endl;
  return res;
}